import socket, sys
client = socket.socket()
client.connect(("localhost", 8080))
while True:
    msg = input(">>:").strip()
    if len(msg) ==0 : continue
    client.send(msg.encode())  # 发送命令
    res_size = client.recv(1024)
    print("返回数据大小", res_size.decode())
    # 发送确认信息,让服务端开始发送数据
    client.send("已确认, 请发送".encode())
    received_size = 0  # 已接收到的数据
    cmd_res = b''
    while received_size <= int(res_size):  # 代表还没收完
        print("-----------")
        data = client.recv(1024)
        received_size += len(data)  # 为什么不是直接1024,还判断len干嘛,注意,实际收到的data有可能比1024少
        cmd_res += data
        print(int(res_size))
        print(received_size)
    else:
        print("数据收完了", received_size)
        f = open("test_copy.html", "wb")  # 把接收到的结果存下来,一会看看收到的数据 对不对
        f.write(cmd_res)  # 把接收到的结果存下来,一会看看收到的数据 对不对
        f.close()
client.close()

